/*
 * Copyright 2017 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.jessyan.lifecyclemodel.demo.slice;

import me.jessyan.lifecyclemodel.demo.MyApplication;
import me.jessyan.lifecyclemodel.demo.ResourceTable;
import me.jessyan.lifecyclemodel.LifecycleModelProviders;
import me.jessyan.lifecyclemodel.LifecycleModelStore;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.ToastDialog;

import java.util.HashMap;

/**
 * ================================================
 * 此框架的主要功能是帮助 Ability / Fraction 存储和管理与 UI 相关的数据, 并且可以避免 Ability 重建时导致的数据丢失
 * 使用方式与向 {@link HashMap} 中 {@link HashMap#put(Object, Object)}, {@link HashMap#get(Object)} 数据一致
 * Demo 中就不再做过多展示
 * <p>
 * Demo 中主要展示两个 Fraction 之间的通讯
 * <p>
 * ================================================
 */
public class MainAbilitySecondSlice extends AbilitySlice {
    private BFraction bFraction;

    /**
     * 初始化
     **/
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main_second);
        Button buttonTwo = (Button) findComponentById(ResourceTable.Id_buttonTwo);
        if (buttonTwo != null) {
            //为按钮设置点击回调
            buttonTwo.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    //添加点击按钮后的事件处理逻辑
                    bFraction.sendFraction();
                }
            });
        }

        //在 Fraction 获取 UserLifecycleModel 之前, 需要将它初始化以及存储起来
        LifecycleModelStore store = LifecycleModelProviders.of((FractionAbility) getAbility(), ResourceTable.Id_stack_layout);
        store.put(UserLifecycleModel.class.getName(), new UserLifecycleModel());
    }

    /**
     * 与用户交互
     **/
    @Override
    public void onActive() {
        super.onActive();
        FractionAbility fractionAbility = (FractionAbility) getAbility();
        AFraction.createAAbilitySlice(fractionAbility.getFractionManager(), ResourceTable.Id_depen);
        bFraction = BFraction.createBAbilitySlice(fractionAbility.getFractionManager(), ResourceTable.Id_depen);
    }

    /**
     * 当重新回到前台时，系统将先调用 onForeground()回调通知开发者，而后 Page 的生命周期状态回到 INACTIVE 状态
     **/
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     * 系统将进行销毁，触发此回调函数
     **/
    @Override
    protected void onStop() {
        super.onStop();
        MyApplication application= (MyApplication) getAbility().getAbilityPackage();
        new ToastDialog(application.getApplicationContext()).setText("AFraction与BFraction销毁").show();
        LifecycleModelProviders.of((FractionAbility) getAbility(), ResourceTable.Id_stack_layout).clear();
    }
}
